﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Dala.Utilities;

namespace Dala.Samples.Flex_Slider_With_Uploadpanel
{
    public partial class homepage_slider : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadImage();
            }
        }
        private void LoadImage()
        {
            try
            {
                const string imagePath = "~/Flex-Slider-With-Uploadpanel/Upload/BannerImage/";
                string imageSource = SiteUtility.SiteBaseUrl + "Flex-Slider-With-Uploadpanel/Upload/BannerImage/";
                string PhotoFilePath = Server.MapPath(imagePath);
                string divSlider = @"<ul class='slides'>";
                string fileSlide = string.Empty;
                string fileName = string.Empty;

                DirectoryInfo di = new DirectoryInfo(PhotoFilePath);
                FileInfo[] rgFiles = di.GetFiles("*.*");
                foreach (FileInfo fi in rgFiles)
                {
                    fileSlide = @"<li><img src='";

                    fileSlide += imageSource + fi + "' /></li>";

                    divSlider += fileSlide;
                }

                divImageSlider.InnerHtml = divSlider + "</ul>";

            }
            catch (Exception ex)
            {
                //ShowErrorMsg("error", ex.Message);
            }
        }

        protected void btnUpload(object sender, EventArgs e)
        {
            if (CheckValidImage(fluImage))
            {
                fluImage.PostedFile.SaveAs(MapPath("~") + "/Flex-Slider-With-Uploadpanel/Upload/BannerImage/" + fluImage.FileName);
                ShowErrorMsg("success", "Image Uploaded Successfully");
            }
        }

        private bool CheckValidImage(FileUpload FileUploadImage)
        {
            string contentType;

            contentType = FileUploadImage.PostedFile.ContentType.ToLower();

            if (contentType == "image/jpg" || contentType == "image/png" || contentType == "image/jpeg")
            {

            }
            else
            {
                ShowErrorMsg("error", "Image format is not valid. Valid image formats are ( jpg, png, jpeg).");

                return false;
            }

            return true;
        }
        private void ShowErrorMsg(string _class, string _msg)
        {
            divMessage.Style.Value = "display:block;";
            divMessage.Attributes.Add("class", _class);
            divMessage.InnerHtml = _msg;
        }
        protected void btnRefresh_Click(object sender, EventArgs e)
        {

        }
    }
}